package com.fram.service.admin.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.entity.IntegralRule;
import com.fram.mapper.IntegralRuleMapper;
import com.fram.service.admin.IntegralRuleSerivce;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class IntegralRuleSerivceImpl implements IntegralRuleSerivce{
	
	@Autowired
	private IntegralRuleMapper integralRuleMapper;
	/**积分规则展示**/
	@Override
	public PageInfo<IntegralRule> list(PageBean<IntegralRule> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<IntegralRule>  list=integralRuleMapper.selectRule(request.getParam());
//		for (IntegralRuleResponse integralRule : list) {
//			integralRule.setRule(JSONTools.jsonStr2Array(integralRule.getRuleGradeJson(),SignIntegralRuleBean.class));
//		}
		if(list == null || list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}
	/****/
	@Override
	public IntegralRule details(Long id)throws ProException {
		IntegralRule news = integralRuleMapper.selectByPrimaryKey(id);
		if(news ==null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return news;
	}
	/**增加**/
	@Override
	public void add(IntegralRule request)throws ProException {
		try {
			if(integralRuleMapper.insertSelective(request)==0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		} catch (DataAccessException e) {
			throw new ProValiDataException(ErrCode.ERR_HAVE_EXISTED.getReturnCode(), ErrCode.ERR_HAVE_EXISTED.getReturnMsg());
		}
	}
	/****/
	@Override
	public void update(IntegralRule request)throws ProException {
		if(integralRuleMapper.updateByPrimaryKeySelective(request)==0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
}
